package com.timoon.sample.web.user;

import java.util.List;

import org.springframework.stereotype.Service;

import com.timoon.app.utils.Constantor;
import com.timoon.component.panel.nav.ResourceMapper;
import com.timoon.sample.dao.TUserDAO;
import com.timoon.sample.dao.model.Resource;
import com.timoon.sample.dao.model.TUser;
import com.timoon.sample.dao.model.TUserExample;

@Service
public class LoginService {

	private ResourceMapper resourceMapper;
	
	@javax.annotation.Resource
	private TUserDAO tUserDAOImpl;
	
	@SuppressWarnings("unchecked")
	public TUser login(final String account,final String password){
		List<TUser> users= this.tUserDAOImpl.selectByExample(new TUserExample(){{
			TUserExample.Criteria ct=this.createCriteria();
			ct.andAccountEqualTo(account);
			ct.andPasswordEqualTo(password);
			ct.andStatusNotEqualTo(Constantor.STATUS_DELETED);
		}});
		
		if(users!=null&&users.size()>0){
			return users.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * 返回一个ResourceMapper
	 * 里面的currentParentResource为null
	 * resourceMap已丰富
	 * @param user
	 * @return
	 */
	public ResourceMapper getResourceMapper(TUser user){
		ResourceMapper mapper=new ResourceMapper();
		
		mapper.registResource(this.getResourcesByUser(user));
		
		return mapper;
	}
	
	/**
	 * 获取当前用户所有资源 
	 * @param user
	 * @return
	 */
	public List<Resource> getResourcesByUser(final TUser user){
		
		//查找角色所对应的资源
		List<Resource> resources=this.tUserDAOImpl.getResourcesByUserId(user);
		
		if(resources.size()==0){
			System.err.println("当前用户没有任何资源");
		}
		
		return resources;
	}
	

	/**
	 * 将当天用户的所有资源在resourceMapper里进行注册
	 * @param resources
	 */
	@SuppressWarnings("unused")
	private void creatResourceMapper(List<Resource> resources){
		if(resources!=null){
			this.resourceMapper.registResource(resources);
		}
	}
	public TUserDAO gettUserDAOImpl() {
		return tUserDAOImpl;
	}

	public void settUserDAOImpl(TUserDAO tUserDAOImpl) {
		this.tUserDAOImpl = tUserDAOImpl;
	}
}
